﻿using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using IBatisNet.DataMapper.MappedStatements;
using IBatisNet.DataMapper.Scope;
using IBatisNet.DataMapper.SessionStore;
using pageHelper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using temphumHis;
using temphumProp;
using temphumState;

namespace temphumProp
{
    public   class temphumPropdao
    {
       // private static readonly object syncObj = new object();
       // private static volatile ISqlMapper mapper = null;
       // private static readonly IDictionary<string, ISqlMapper> dictMappers = null;
        //static  plcsetupdao() { dictMappers = new Dictionary<string, ISqlMapper>(); }
           
        private  ISqlMapper mapper;
        public temphumPropdao(ISqlMapper mapper) {
            this.mapper = mapper;
        }
        //private static temphumPropdao dao = new temphumPropdao();
        //public static ISqlMapper SqlMap
        //{
        //    //get { return GetMapper("IBatisNetTest.Config.SqlMap.config", "IBatisNetTest"); } //程序集中加载
        //    get { return GetMapper("DataMap/Config/SqlMap.config", ""); }
        //}
        //public static temphumPropdao GettemphumPropdao() {
        //    Console.WriteLine(111);
        //    return dao;
        //}
        //获取PLC设备的列表
        public IList<temphumProperty> SelectAllProperties()
        {
           
            IList<temphumProperty> ListPlcsetup = mapper.QueryForList<temphumProperty>("SelectAllProp", null);  //获取PLC设备的列表
            return ListPlcsetup;
        }
        public void SaveProperty(temphumProperty property)
        {
          mapper.Insert("SaveProperty",property);
        }
        public void updateProperty(temphumProperty property)
        {
            mapper.Update("updateProperty", property);
        }
        public void deleteProperty(temphumProperty property)
        {
            mapper.Delete("deleteProperty", property);
        }
        public void saveHistoryData(temphumHisData data)
        {
            mapper.Insert("saveHistoryData", data);
        }
        public void saveTestState(temphumTestState data)
        {
            mapper.Insert("saveTestState", data);
        }
        public void deleteAllTestState()
        {
            mapper.Delete("deleteAllTestState", null);
        }
        public void deleteTestStateByPort(string testport)
        {
            mapper.Delete("deleteTestStateByPort", testport);
        }
        public temphumHisData findDataBySN(string serialnumber) {
          return  mapper.QueryForObject<temphumHisData>("findDataBySN", serialnumber);
        }
        public IList<temphumTestState> findAllTestState()
        {
            return mapper.QueryForList<temphumTestState>("findAllTestState", null);
        }
        public long findHisDataCount(pageLink link)
        {
            return mapper.QueryForObject<long>("findHisDataCount", link);
        }
        public long findCountBySN(pageLink link)
        {
            return mapper.QueryForObject<long>("findCountBySN", link);
        }
        public IList<temphumHisData> findAllHisData(pageLink link)
        {
            Console.WriteLine(222);
            return mapper.QueryForList<temphumHisData>("findAllHisData", link);
        }
        public pager<temphumHisData> findByPager(pageLink link) {
            Console.WriteLine(link.ToString());
            pager<temphumHisData> page = new pager<temphumHisData>();
            page.size = link.size;
            page.total = findHisDataCount(link);
            page.page = link.page;
            page.data = findAllHisData(link);
            return page;
        }
        public IList<temphumHisData> findByPaginated(pageLink link)
        {
            IList<temphumHisData> list = mapper.QueryForList<temphumHisData>("findAllHisData", link, (link.page-1) * link.size,link.size);
            return list;
        }
        //public static string GetRuntimeSql(ISqlMapper sqlMapper, string statementName, object paramObject)
        //{
        //    string result = string.Empty;
        //    try
        //    {
        //        IMappedStatement statement = sqlMapper.GetMappedStatement(statementName);
        //        if (!sqlMapper.IsSessionStarted)
        //        {
        //            sqlMapper.OpenConnection();
        //        }
        //        RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, paramObject, sqlMapper.LocalSession);
        //        result = scope.PreparedStatement.PreparedSql;
        //    }
        //    catch (Exception ex)
        //    {
        //        result = "获取SQL语句出现异常:" + ex.Message;
        //    }
        //    return result;
        //}
        //public static ISqlMapper GetMapper(string mapperName, string assemblyName)
        //{
        //    if (string.IsNullOrEmpty(mapperName))
        //    {
        //        throw new Exception("MapperName为空!");
        //    }

        //    if (dictMappers.ContainsKey(mapperName))
        //    {
        //        mapper = dictMappers[mapperName];
        //    }
        //    else
        //    {
        //        if (mapper == null)
        //        {
        //            lock (syncObj)
        //            {
        //                if (mapper == null)
        //                {
        //                    if (string.IsNullOrEmpty(assemblyName))
        //                    {
        //                        #region 从本地配置文件中生成SqlMapper
        //                        mapper = new DomSqlMapBuilder().Configure(mapperName);
        //                        mapper.SessionStore = new HybridWebThreadSessionStore(mapper.Id);
        //                        dictMappers.Add(mapperName, mapper);
        //                        #endregion
        //                    }
        //                    else
        //                    {
        //                        #region 从资源中生成SqlMapper
        //                        Assembly assembly = Assembly.Load(assemblyName);
        //                        Stream stream = assembly.GetManifestResourceStream(mapperName);
        //                        DomSqlMapBuilder builder = new DomSqlMapBuilder();
        //                        mapper = builder.Configure(stream);
        //                        dictMappers.Add(mapperName, mapper);
        //                        #endregion
        //                    }
        //                }
        //            }
        //        }
        //    }
        //    return mapper;
        //}
    }


}
